package algame;

import static org.lwjgl.opengl.GL11.*;
import java.io.FileInputStream;
import java.io.DataInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

class MeshLoader {

	private static float[][] vertici = new float[5000][5];
	private static int totvertici = 0;
	private static float[] pos = new float[3];
	private static float[] rot = new float[4];
	private static  float[] col = new float[3];
	private static float[] sca = new float[3];

	public static void readmtl(String filename) {
		try {
			FileInputStream fis = new FileInputStream(filename);
			DataInputStream dis = new DataInputStream(fis);
			BufferedReader br = new BufferedReader(new InputStreamReader(dis));
			String strbuf;
			String curdir = "";
			String strarr[];
			String strarr2[];
			int totmat = 0;

			while((strbuf = br.readLine()) != null) {

				if(strbuf.startsWith("newmtl ")) {
					totmat++;
				} else if(strbuf.startsWith("Ns ")) {
					float Ns = 0.0f;
					Ns = Float.parseFloat(strbuf.substring(3));
				} else if(strbuf.startsWith("Ka ")) {
					float[] Ka = new float[3];
					strarr = strbuf.split(" ");
					Ka[0] = Float.parseFloat(strarr[1]);
					Ka[1] = Float.parseFloat(strarr[2]);
					Ka[2] = Float.parseFloat(strarr[3]);

				} else if(strbuf.startsWith("Kd ")) {
					float[] Kd = new float[3];
					strarr = strbuf.split(" ");
					Kd[0] = Float.parseFloat(strarr[1]);
					Kd[1] = Float.parseFloat(strarr[2]);
					Kd[2] = Float.parseFloat(strarr[3]);

				} else if(strbuf.startsWith("Ks ")) {
					float[] Ks = new float[3];
					strarr = strbuf.split(" ");
					Ks[0] = Float.parseFloat(strarr[1]);
					Ks[1] = Float.parseFloat(strarr[2]);
					Ks[2] = Float.parseFloat(strarr[3]);

				} else if(strbuf.startsWith("Ni ")) {
					float Ni = 0.0f;
					Ni = Float.parseFloat(strbuf.substring(3));

				} else if(strbuf.startsWith("d ")) {
					float d = 0.0f;
					d = Float.parseFloat(strbuf.substring(2));

				} else if(strbuf.startsWith("illum ")) {
					float illum = 0.0f;
					illum = Float.parseFloat(strbuf.substring(6));

				} else if(strbuf.startsWith("map_Kd ")) {
					String map_Kd = "";
					map_Kd = strbuf.substring(7);
				}
			}
			System.out.println("Caricati " + totmat + " materiali.");
			dis.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	public static void load(String filename, String nome) {
		try {
			FileInputStream fis = new FileInputStream(filename);
			DataInputStream dis = new DataInputStream(fis);
			BufferedReader br = new BufferedReader(new InputStreamReader(dis));
			String strbuf;
			String curdir = "";
			String strarr[];
			String strarr2[];
			float[][] v= new float[5000][3];
			int totv = 1;
			float[][] vt= new float[5000][3];
			int totvt = 1;
			int v1, v2, v3, v4;
			int vt1, vt2, vt3, vt4;
			int tottri = 0;

			boolean reading = false;
			boolean stop = false;

//		mtllib stanza.mtl


			while((strbuf = br.readLine()) != null && stop != true) {
				if(strbuf.startsWith("mtllib ")) {
					strarr = filename.split("/");
					for(int i = 0; i < strarr.length -1; i++) {
						curdir+=strarr[i];
					}
					readmtl(curdir + "/" + strbuf.substring(7));
					System.out.println("Libreria " + curdir + "/" + strbuf.substring(7));
				} else if(strbuf.startsWith("o ")) {
					System.out.println("Oggetto " + strbuf.substring(2));
					if(!nome.equals("all")) {
						if(strbuf.substring(2).equals(nome)) {
							reading = true;
						} else {
							stop = reading;
							reading = false;
						}
					} else {
						reading = true;
						stop = false;
					}
				} else if(strbuf.startsWith("v ")) {
					strarr = strbuf.split(" ");
					v[totv][0] = Float.parseFloat(strarr[1]);
					v[totv][1] = Float.parseFloat(strarr[2]);
					v[totv][2] = Float.parseFloat(strarr[3]);
					totv++;
				} else if(strbuf.startsWith("vt ")) {
					strarr = strbuf.split(" ");
					vt[totvt][0] = Float.parseFloat(strarr[1]);
					vt[totvt][1] = 1-Float.parseFloat(strarr[2]);
					totvt++;
				} else if(strbuf.startsWith("f ") && reading) {
					strarr = strbuf.split(" ");
					if(strbuf.indexOf("/") == -1) {
						v1 = Integer.parseInt(strarr[1]);
						v2 = Integer.parseInt(strarr[2]);
						v3 = Integer.parseInt(strarr[3]);
						v4 = Integer.parseInt(strarr[4]);

						vt1 = 0;
						vt2 = 0;
						vt3 = 0;
						vt4 = 0;
					} else {
						v1 = Integer.parseInt(strarr[1].split("/")[0]);
						v2 = Integer.parseInt(strarr[2].split("/")[0]);
						v3 = Integer.parseInt(strarr[3].split("/")[0]);
						v4 = Integer.parseInt(strarr[4].split("/")[0]);

						vt1 = Integer.parseInt(strarr[1].split("/")[1]);
						vt2 = Integer.parseInt(strarr[2].split("/")[1]);
						vt3 = Integer.parseInt(strarr[3].split("/")[1]);
						vt4 = Integer.parseInt(strarr[4].split("/")[1]);
					}

					addVertex(v[v1][0], v[v1][1], v[v1][2], vt[vt1][0], vt[vt1][1]);
					addVertex(v[v2][0], v[v2][1], v[v2][2], vt[vt2][0], vt[vt2][1]);
					addVertex(v[v3][0], v[v3][1], v[v3][2], vt[vt3][0], vt[vt3][1]);

					addVertex(v[v1][0], v[v1][1], v[v1][2], vt[vt1][0], vt[vt1][1]);
					addVertex(v[v3][0], v[v3][1], v[v3][2], vt[vt3][0], vt[vt3][1]);
					addVertex(v[v4][0], v[v4][1], v[v4][2], vt[vt4][0], vt[vt4][1]);

					tottri += 2;

				}
			}
			System.out.println("Caricati " + totv + " coord,  " + totvt + " texcoord, " + (tottri + 1) + " facce.");
			dis.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	public static void addVertex(float vx,float vy,float vz,float tx,float ty) {
		vertici[totvertici][0] = vx;
		vertici[totvertici][1] = vy;
		vertici[totvertici][2] = vz;
		vertici[totvertici][3] = tx;
		vertici[totvertici][4] = ty;
		totvertici++;
	}

	public static void scol(float vr, float vg, float vb) {
		col[0] = vr;
		col[1] = vg;
		col[2] = vb;
	}

	public static void spos(float vx, float vy, float vz) {
		pos[0] = vx;
		pos[1] = vy;
		pos[2] = vz;
	}

	public static void srot(float value, float vx, float vy, float vz) {
		rot[0] = value;
		rot[1] = vx;
		rot[2] = vy;
		rot[3] = vz;
	}

	public static void ssca(float sx, float sy, float sz) {
		sca[0] = sx;
		sca[1] = sy;
		sca[2] = sz;
	}

	public static void render(int meshid) {

		glEnable(GL_TEXTURE_2D);

		glPushMatrix();

		glScalef(sca[0],sca[1],sca[2]);
		glRotatef(rot[0], rot[1], rot[2], rot[3]);
		glTranslatef(pos[0], pos[1], pos[2]);

		glColor3f(col[0],col[1],col[2]);

		glBegin(GL_TRIANGLES);

		for(int i = 0; i < totvertici; i++) {
			glTexCoord2f(vertici[i][3], vertici[i][4]);
			glVertex3f(vertici[i][0], vertici[i][1], vertici[i][2]);
		}

		glEnd();

		glPopMatrix();
	}

}
